package cc.rengu.redp.system.controller;


import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.utils.RedpUpmpUtil;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.properties.RedpProperties;
import cc.rengu.redp.common.utils.RedpUtil;
import cc.rengu.redp.common.utils.SpringContextUtil;
import cc.rengu.redp.system.domain.UserMessage;
import cc.rengu.redp.system.service.CommonUserMessageService;
import cc.rengu.redp.system.service.UserMessageService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xushitang
 */
@Slf4j
@RestController
@RequestMapping("/admin/userMessage")
public class UserMessageController extends BaseController {

    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private CommonUserMessageService commonUserMessageService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    private static Integer userMessageInterval = SpringContextUtil.getBean(RedpProperties.class).getUserMessageInterval();

    @Log("查询消息")
    @GetMapping
    public Map<String, Object> userList(QueryRequest queryRequest, String[] range, String sourceType) {
        return getDataTable(userMessageService.findAllMessage(range, sourceType, RedpUtil.getCurrentUser().getUserId(), queryRequest));
    }

    @Log("删除消息")
    @DeleteMapping("/{ids}")
    public RedpResponse delete(@PathVariable String ids) {
        try {
            String[] idsArr = ids.split(StringPool.COMMA);
            userMessageService.getBaseMapper().deleteBatchIds(Arrays.asList(idsArr));
        } catch (Exception e) {
            String message = "删除消息失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("删除消息成功");
    }

    @Log("已阅消息")
    @GetMapping("read/{id}")
    public RedpResponse read(@PathVariable Long id) {
        try {
            userMessageService.read(id);
        } catch (Exception e) {
            String message = "已阅消息设置失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("已阅消息设置成功");
    }

    @Log("发送消息")
    @PutMapping
    public RedpResponse sendMesssage(String userNames, String message) {
        try {
            String[] userNameArr = userNames.split(StringPool.COMMA);
            for (String userName : userNameArr) {
                commonUserMessageService.sendMessage(userName, message, UserMessage.SOURCE_TYPE_USER, null);
            }
        } catch (Exception e) {
            String errMsg = "发送消息失败";
            log.error(errMsg, e);
            return RedpResponse.getInstance().failure().message(errMsg);
        }

        return RedpResponse.getInstance().success().message("发送消息成功");
    }

    @GetMapping("getUnreadMessageCount")
    public Map<String, Object> getUnreadMessageCount() {
        UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
        Integer cnt = userMessageService.getUnreadMessageCount(Long.valueOf(RedpUpmpUtil.getCurrentUser(defaultInst.getPrimaryValue()).getUserId()));
        Map<String, Object> rs = new HashMap<>();
        rs.put("count", cnt);
        rs.put("interval", userMessageInterval * 1000);
        return rs;
    }
}
